package com.ruoyi.project.module.movie.controller;

import com.alibaba.fastjson.JSON;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.module.movie.domain.Movie;
import com.ruoyi.project.module.movie.service.IMovieService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;

/**
 * 电影 信息操作处理
 * 
 * @author ruoyi
 * @date 2018-11-13
 */
@Controller
@RequestMapping("/module/movie")
public class MovieController extends BaseController
{
    private String prefix = "module/movie";
	
	@Autowired
	private IMovieService movieService;
	
	@RequiresPermissions("module:movie:view")
	@GetMapping()
	public String movie()
	{
	    return prefix + "/movie";
	}
	
	/**
	 * 查询电影列表
	 */
	@RequiresPermissions("module:movie:list")
	@PostMapping("/list")
	@ResponseBody
	public TableDataInfo list(Movie movie) {
//		Movie movie = new Movie();
//		movie.setMovieTitle(req.getParameter("movieTitle"));
//		movie.setRemark(req.getParameter("remark"));
//		movie.setMovieScore(req.getParameter("movieScore"));
//		if (req.getParameter("movieRelesdate") != null) {
//			try {
//				movie.setMovieRelesdate(new SimpleDateFormat("yyyy-MM-dd").parse(req.getParameter("movieRelesdate")));
//			} catch (ParseException e) {
//				e.printStackTrace();
//			}
//		}
		//movie.setMovieState(0);
			startPage();
			List<Movie> list = movieService.selectMovieList(movie);
			return getDataTable(list);
		}

	/**
	 * 新增电影
	 */
	@GetMapping("/add")
	public String add()
	{
	    return prefix + "/add";
	}
	
	/**
	 * 新增保存电影
	 */
	@RequiresPermissions("module:movie:add")
	@Log(title = "电影", businessType = BusinessType.INSERT)
	@PostMapping("/add")
	@ResponseBody
	public AjaxResult addSave(Movie movie)
	{

		return toAjax(movieService.insertMovie(movie));
	}

	/**
	 * 修改电影
	 */
	@GetMapping("/edit/{movieId}")
	public String edit(@PathVariable("movieId") Integer movieId, ModelMap mmap)
	{
		Movie movie = movieService.selectMovieById(movieId);
		mmap.put("movie", movie);
	    return prefix + "/edit";
	}
	
	/**
	 * 修改保存电影
	 */
	@RequiresPermissions("module:movie:edit")
	@Log(title = "电影", businessType = BusinessType.UPDATE)
	@PostMapping("/edit")
	@ResponseBody
	public AjaxResult editSave(Movie movie)
	{		
		return toAjax(movieService.updateMovie(movie));
	}
	
	/**
	 * 删除电影
	 */
	@RequiresPermissions("module:movie:remove")
	@Log(title = "电影", businessType = BusinessType.DELETE)
	@PostMapping( "/remove")
	@ResponseBody
	public AjaxResult remove(String ids)
	{		
		return toAjax(movieService.deleteMovieByIds(ids));
	}
	@Log(title = "电影信息", businessType = BusinessType.EXPORT)
	@RequiresPermissions("system:user:export")
	@PostMapping("/export")
	@ResponseBody
	public AjaxResult export(Movie movie) throws Exception
	{

		try
		{
			List<Movie> list = movieService.selectMovieList(movie);
			ExcelUtil<Movie> util = new ExcelUtil<Movie>(Movie.class);
			return util.exportExcel(list, "Movie");
		}
		catch (Exception e)
		{
			return error("导出Excel失败，请联系网站管理员！");
		}
	}
	@RequiresPermissions("module:movie:shangjia")
	@Log(title = "电影上架", businessType = BusinessType.UPDATE)
	@PostMapping( "/shangjia")
	@ResponseBody
	public Object shangjias(HttpServletRequest req)
	{
	    String id=req.getParameter("id");
		Movie movie=movieService.selectMovieById(Integer.valueOf(id));
		String status="";
		int num=movie.getMovieState();
		if(num==1){
			status="审核中,不能上架";
		}else if(num==2){
			status="审核未通过,不能上架";
		}else if(num==3){
			status="未审核,不能上架";
		}else if(num==4){
			status="审核未通过,不能上架";
		}else if(num==5){
			status="已上架,不能上架";
		}else{
        int row=movieService.updateStatus(Integer.valueOf(id));
        if(row>0){
			status="上架成功";
		}
		}
		System.out.print("null");
		System.out.print("null");
		return JSON.toJSON(status);
	}
}
